<?php
class Pengajar_con extends CI_Controller{
        
        function __construct() {
            parent::__construct();
            $this->load->library('wysiwyg');
        }
        function index(){
            //menampilkan halaman default
            //rencananya akan menampilkan tabel kelas yang ada dengan status belum (artinya baru ditawarkan)
            $this->tampil_pengajar();
        }

        function tampil_pengajar(){
            $query = $this->db->query("SELECT * FROM user INNER JOIN pengajar ON user.ID=pengajar.ID");
            $row=$query->result_array();
            $data2['data']=$row;
            $data['script']=$this->wysiwyg->loadScript();   //form akan menggunakan textarea WYSIWYG
            $data['title']="Tampil Pengajar";
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/pengajar_display',$data2);
            $this->load->view('footer_view');
        }

        function tambah_pengajar(){
            //nambah pertanyaan
            // menambah pertanyaan
            $data['script']=$this->wysiwyg->loadScript();   //form akan menggunakan textarea WYSIWYG
            $data['title']="Tambah Pengajar";
            $data2['mode'] = null;
            $data2['data1'] = null;
            $data2['data2'] = null;
            $data2['error'] = $this->session->userdata('error');
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/pengajar_form',$data2);

            $this->load->view('footer_view');
            $this->session->unset_userdata('error');
        }

        function simpan_tambah_pengajar(){
            //melakukan insert database pertanyaan dan daftar pilihan
           
            $nama = $this->input->post('nama');
            $data['mode'] = "";
            $data['script']=$this->wysiwyg->loadScript();   //form akan menggunakan textarea WYSIWYG
            $data['title']="Tambah Pengajar";
            if($nama == null)
                {
                    $this->session->set_userdata('error', 'isikan nama');
                    redirect(base_url() . 'admin/pengajar_con/tambah_pengajar');
                }
            $username = $this->input->post('username');
            $password = md5($this->input->post('password'));
            if($username == null || $password == null)
                {
                    $this->session->set_userdata('error', 'username dan password tidak boleh kosong');
                    redirect(base_url() . 'admin/pengajar_con/tambah_pengajar');
                }
            $email = $this->input->post('e-mail');
            $alamat = $this->input->post('alamat');
            $notelp =$this->input->post('notelp');
            //$foto = $_FILES['foto'];
            $rateinstruktur = $this->input->post('rate_instruktur');
            $rateasisten =  $this->input->post('rate_asisten');

            $alamatfoto = "";
            mkdir('./foto/'. $username);
            $alamatawal = 'foto/'. $username;
            $config['upload_path'] = './'.$alamatawal;
            $config['overwrite'] = true;
            $alamatfoto = $config['upload_path'];
            $config['file_name'] = $this->input->post('username');
            $config['allowed_types'] = 'gif|jpg|png';

            $this->load->library('upload', $config);
            if ( ! $this->upload->do_upload('foto')){
            }else{
                $foto = $this->upload->data();
                $alamatfoto = base_url().$alamatawal.'/'.$config['file_name'].$foto['file_ext'];
            }
            $this->db->insert('user', array('USERNAME' => $username, 'PASSWORD' => $password, 'NAMA' => $nama, 'EMAIL' => $email, 'ALAMAT' => $alamat, 'NO_TELP' => $notelp, 'FOTO' => $alamatfoto));
            $query = $this->db->query('SELECT MAX(ID) AS ID FROM user');
            $row = $query->result_array();
            $id = $row[0]['ID'];
            $this->db->insert('pengajar', array('RATE_INS'=> $rateinstruktur,'RATE_ASS'=>$rateasisten,'ID'=>$id));
            $this->db->insert('role',array('ID'=>$id,'ID_ROLE'=>8));
            redirect(base_url() . 'admin/pengajar_con/');
        }
        function edit_pengajar($id){
            //melakukan edit pertanyaan
            $query = $this->db->get_where('user',array ('ID'=>$id));
            $row = $query->result_array();
            $data2['data1'] = $row[0];
            $query = $this->db->get_where('pengajar',array ('ID'=>$id));
            $row = $query->result_array();
            $data2['data2'] = $row[0];
            $data['script'] = $this->wysiwyg->loadScript();   //form akan menggunakan textarea WYSIWYG
            $data['title']="Edit Pengajar";
            $data2['mode'] = "edit";
            $data2['error'] = $this->session->userdata('error');
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/pengajar_form',$data2);
            $this->load->view('footer_view');
        }

        function simpan_edit_pengajar($id){
            //simpan edit pertanyaan
            $nama = $this->input->post('nama');
            $username = $this->input->post('username');
            
            $email = $this->input->post('e-mail');
            $alamat = $this->input->post('alamat');
            $notelp =$this->input->post('notelp');
            $rateinstruktur = $this->input->post('rate_instruktur');
            $rateasisten =  $this->input->post('rate_asisten');
            $query = $this->db->query('UPDATE user SET
                NAMA="'. $nama .'", USERNAME="'. $username .'"
                , EMAIL="'. $email .'" , ALAMAT="'. $alamat .'"
                , NO_TELP="'. $notelp .'" WHERE ID = '.$id);
            if($this->input->post('password')!=null)
               {
                    $password = md5($this->input->post('password'));
                    $query = $this->db->query('UPDATE user SET
                    PASSWORD="'. $password .'" WHERE ID = '.$id);
                }
            $query = $this->db->query('UPDATE pengajar SET
                RATE_INS="'. $rateinstruktur .'"
                , RATE_ASS="'. $rateasisten .'" WHERE ID = '.$id);
            redirect(base_url().'admin/pengajar_con');
        }

        function edit_pengajar_foto($id)
        {
            $data['script'] = $this->wysiwyg->loadScript();   //form akan menggunakan textarea WYSIWYG
            $data['title']="Edit Pengajar";
            $data2['id'] = $id;
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/pengajar_edit_foto',$data2);
            $this->load->view('footer_view');
        }

        function  simpan_edit_pengajar_foto($id){
            $query = $this->db->query("SELECT * FROM user WHERE ID=$id");
            $row=$query->row_array();
            $nama_file = $row['USERNAME'];
            $file_ext =  substr($_FILES['foto']['name'], strlen($_FILES['foto']['name'])-4, 4);
            $config['file_name']=$row['USERNAME'].$file_ext;
            if(!is_dir('./foto/'. $row['USERNAME'])){
                mkdir('./foto/'. $row['USERNAME']);
            }
            $config['upload_path'] = './foto/'.$row['USERNAME'];
            $config['overwrite'] = true;
            $config['allowed_types'] = 'gif|jpg|png';
            
            
            $this->load->library('upload', $config);
            if ( !$this->upload->do_upload('foto')){
                echo $this->upload->display_errors().'<br/>Pilih back';
            }else{
                $dataupload=$this->upload->data();
                $absolute_path= base_url().'foto/'.$row['USERNAME'].'/'.$dataupload['file_name'];
                $query = $this->db->query('UPDATE user SET FOTO="'. $absolute_path .'" WHERE ID = '.$id);
            
                redirect(base_url().'admin/pengajar_con');
            }
            
        }

        function hapus_pengajar($id){
            //menghapus pertanyaan dengan id=$id
            $this->db->delete('user', array('ID' => $id));
            $this->db->delete('pengajar', array('ID' => $id));
            redirect(base_url().'admin/pengajar_con');
        }

        function profil_pengajar($id){
            $data['script'] = $this->wysiwyg->loadScript();   //form akan menggunakan textarea WYSIWYG
            $data['title']="Edit Pengajar";
            $query = $this->db->query("SELECT * FROM user INNER JOIN pengajar ON user.ID=pengajar.ID  WHERE user.ID=".$id);
            $row=$query->result_array();
            $data2 = $row[0];
            $this->load->view('admin/admin_header_view', $data);
            $this->load->view('admin/profil_pengajar',$data2);
            $this->load->view('footer_view');
        }

        function cari_urut() {
            $query = $this->db->query('SELECT count(*) as jumlah FROM role where ID_ROLE=8');
            $result = $query->result_array();
            return ($result[0]['jumlah'] + 1);
        }
        
    }

?>
